package br.com.insejeccg.util.bean.convert;

import java.lang.reflect.Array;

import org.apache.commons.beanutils.Converter;

@SuppressWarnings("unchecked")
public class IntegerConverter implements Converter {
    private  org.apache.commons.beanutils.converters.IntegerConverter integerConverter;

    public IntegerConverter(Object defaultValue) {
        integerConverter = new org.apache.commons.beanutils.converters.IntegerConverter(defaultValue);
    }

    public Object convert(Class type, Object value) {
        if ((value != null) && value.getClass().isArray()) {
            if (Array.getLength(value) == 0) {
                return null;
            } else {
                value = Array.get(value, 0);
            }
        }

        return integerConverter.convert(type, value);
    }
}
